namespace SinaFinanceBaseClassLibrary
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;

    public static class DB_SinaServiceHelp
    {
        public static DB_KLine GetKLineData(string resultXml)
        {
            DB_KLine line = null;
            XElement element = XElement.Parse(resultXml);
            List<DB_KLine> list = new List<DB_KLine>
                (from sn in element.Elements("info")
                 select new DB_KLine
                         {
                             code = sn.Element("code").Value.Trim(),
                             exchange = sn.Element("exchange").Value.Trim(),
                             name = sn.Element("name").Value.Trim(),
                             symbol = sn.Element("symbol").Value.Trim(),
                             type = sn.Element("type").Value.Trim(),
                             version = sn.Element("version").Value.Trim()
                         });
            if (list.Count != 0)
            {
                line = list[0];
                IEnumerable<DB_KLineData> enumerable2 =
                    from sn in element.Elements("data").Elements<XElement>("item")
                    select new DB_KLineData
                               {
                                   Close = sn.Element("close") == null ? "" : sn.Element("close").Value,
                                   Day = sn.Element("day") == null ? "" : sn.Element("day").Value,
                                   High = sn.Element("high") == null ? "" : sn.Element("high").Value,
                                   Low = sn.Element("low") == null ? "" : sn.Element("low").Value,
                                   Open = sn.Element("open") == null ? "" : sn.Element("open").Value,
                                   Price15 = sn.Element("price15") == null ? "" : sn.Element("price15").Value,
                                   Price30 = sn.Element("price30") == null ? "" : sn.Element("price30").Value,
                                   Price5 = sn.Element("price5") == null ? "" : sn.Element("price5").Value,
                                   Volume = sn.Element("volume") == null ? "" : sn.Element("volume").Value,
                                   Volume15 = sn.Element("volume") == null ? "" : sn.Element("volume").Value,
                                   Volume30 = sn.Element("volume30") == null ? "" : sn.Element("volume30").Value,
                                   Volume5 = sn.Element("volume5") == null ? "" : sn.Element("volume5").Value
                               };
                line.Data = new List<DB_KLineData>(enumerable2);
            }
            return line;
        }

        public static List<DB_RealTime> GetMarketBaseData(string resultXml)
        {
            XElement element = XElement.Parse(resultXml);
            IEnumerable<DB_RealTime> source = from sn in element.Elements("item") select new DB_RealTime();
            if (source.ToList<DB_RealTime>().Count == 1)
            {
                if (string.IsNullOrEmpty(((XElement) element.FirstNode).Value))
                {
                    return null;
                }
            }
            else if ((source.ToList<DB_RealTime>().Count == 2) && (string.IsNullOrEmpty(((XElement) element.FirstNode).Value) || string.IsNullOrEmpty(((XElement) element.LastNode).Value)))
            {
                return null;
            }
            return new List<DB_RealTime>(from sn in element.Elements("item") select new DB_RealTime { 
                Version = sn.Element("version").Value.Trim(), Exchange = sn.Element("exchange").Value.Trim(), Type = sn.Element("type").Value.Trim(), Symbol = sn.Element("symbol").Value.Trim(), Code = sn.Element("code").Value.Trim(), Name = sn.Element("name").Value.Trim(), Trade = sn.Element("trade").Value.Trim(), PrevTrade = sn.Element("prevtrade").Value.Trim(), TradeOffset = sn.Element("tradeoffset").Value.Trim(), PriceChange = sn.Element("pricechange").Value.Trim(), ChangePercent = sn.Element("changepercent").Value.Trim(), PriceSwing = sn.Element("priceswing").Value.Trim(), SwingPercent = sn.Element("swingpercent").Value.Trim(), Settlement = sn.Element("settlement").Value.Trim(), Open = sn.Element("open").Value.Trim(), High = sn.Element("high").Value.Trim(), 
                Low = sn.Element("low").Value.Trim(), Buy = sn.Element("buy").Value.Trim(), Sell = sn.Element("sell").Value.Trim(), Volumn = sn.Element("volume").Value.Trim(), Amount = sn.Element("amount").Value.Trim(), Opendate = sn.Element("opendate").Value.Trim(), TickTime = sn.Element("ticktime").Value.Trim(), UpdateTime = sn.Element("updatetime").Value.Trim(), BidPrice1 = sn.Element("bidprice1").Value.Trim(), BidPrice2 = sn.Element("bidprice2").Value.Trim(), BidPrice3 = sn.Element("bidprice3").Value.Trim(), BidPrice4 = sn.Element("bidprice4").Value.Trim(), BidPrice5 = sn.Element("bidprice5").Value.Trim(), BidVolume1 = sn.Element("bidvolume1").Value.Trim(), BidVolume2 = sn.Element("bidvolume2").Value.Trim(), BidVolume3 = sn.Element("bidvolume3").Value.Trim(), 
                BidVolume4 = sn.Element("bidvolume4").Value.Trim(), BidVolume5 = sn.Element("bidvolume5").Value.Trim(), AskPrice1 = sn.Element("askprice1").Value.Trim(), AskPrice2 = sn.Element("askprice2").Value.Trim(), AskPrice3 = sn.Element("askprice3").Value.Trim(), AskPrice4 = sn.Element("askprice4").Value.Trim(), AskPrice5 = sn.Element("askprice5").Value.Trim(), AskVolume1 = sn.Element("askvolume1").Value.Trim(), AskVolume2 = sn.Element("askvolume2").Value.Trim(), AskVolume3 = sn.Element("askvolume3").Value.Trim(), AskVolume4 = sn.Element("askvolume4").Value.Trim(), AskVolume5 = sn.Element("askvolume5").Value.Trim(), CurrentVolume = sn.Element("currentVolume").Value.Trim()
             });
        }

        public static Dictionary<string, List<DB_MinLine>> GetMinLineList(string resultXml)
        {
            if (string.IsNullOrEmpty(resultXml))
            {
                return null;
            }
            XElement element = XElement.Parse(resultXml);
            Dictionary<string, List<DB_MinLine>> dictionary = new Dictionary<string, List<DB_MinLine>>();
            int num = element.Nodes().Count<XNode>();
            if (num.Equals(2))
            {
                string name = ((XElement) element.FirstNode).Name.ToString();
                string str2 = ((XElement) element.LastNode).Name.ToString();
                IEnumerable<DB_MinLine> enumerable = from sn in element.Elements(name).Elements<XElement>("item") select new DB_MinLine { symbol = sn.Element("symbol").Value.Trim(), mintime = sn.Element("mintime").Value.Trim(), price = sn.Element("price").Value.Trim(), volume = sn.Element("volume").Value.Trim(), avg_price = sn.Element("avg_price").Value.Trim() };
                IEnumerable<DB_MinLine> enumerable2 = from sn in element.Elements(str2).Elements<XElement>("item") select new DB_MinLine { symbol = sn.Element("symbol").Value.Trim(), mintime = sn.Element("mintime").Value.Trim(), price = sn.Element("price").Value.Trim(), volume = sn.Element("volume").Value.Trim(), avg_price = sn.Element("avg_price").Value.Trim() };
                List<DB_MinLine> list = new List<DB_MinLine>(enumerable);
                List<DB_MinLine> list2 = new List<DB_MinLine>(enumerable2);
                dictionary.Add(name, list);
                dictionary.Add(str2, list2);
                return dictionary;
            }
            if (num.Equals(1))
            {
                string str3 = ((XElement) element.FirstNode).Name.ToString();
                IEnumerable<DB_MinLine> enumerable3 = from sn in element.Elements(str3).Elements<XElement>("item") select new DB_MinLine { symbol = sn.Element("symbol").Value.Trim(), mintime = sn.Element("mintime").Value.Trim(), price = sn.Element("price").Value.Trim(), volume = sn.Element("volume").Value.Trim(), avg_price = sn.Element("avg_price").Value.Trim() };
                dictionary.Add(str3, new List<DB_MinLine>(enumerable3));
            }
            return dictionary;
        }

        public static List<DB_News> GetNewsInfoData(string resultXml)
        {
            if (!string.IsNullOrEmpty(resultXml))
            {
                List<string> list = resultXml.Substring((int) (resultXml.IndexOf('=') + 1)).Replace("[\n[", string.Empty).Replace("]\n]", string.Empty).Replace("[\"", "\"").Replace("\"],", "\",&").Replace("\"", string.Empty).Split(new char[] { '&' }).ToList<string>();
                if (list.Count <= 1)
                {
                    return null;
                }
                List<DB_News> list2 = new List<DB_News>();
                for (int i = 0; i < list.Count; i++)
                {
                    string[] strArray = list[i].Split(new char[] { ',' });
                    list2.Add(new DB_News { Date = strArray[0], NewsID = strArray[1], Title = strArray[2], NewsHref = strArray[3], Symbol = strArray[4] });
                }
                return list2;
            }
            return null;
        }

        public static List<DB_SUGGEST> GetSuggestData(string resultXml)
        {
            if (!string.IsNullOrEmpty(resultXml))
            {
                string str = resultXml.Substring((int) (resultXml.IndexOf('=') + 1), (int) ((resultXml.LastIndexOf(';') - (resultXml.IndexOf('=') + 1)) - 2)).Replace("\"", string.Empty);
                if (string.IsNullOrEmpty(str))
                {
                    return null;
                }
                List<string> list = str.Split(new char[] { ';' }).ToList<string>();
                List<DB_SUGGEST> list2 = new List<DB_SUGGEST>();
                for (int i = 0; i < list.Count; i++)
                {
                    string[] strArray = list[i].Split(new char[] { ',' });
                    list2.Add(new DB_SUGGEST { Matching = strArray[0], StockType = strArray[1], Symbol6 = strArray[2], Symbol8 = strArray[3], Name = strArray[4], PinYin = strArray[5] });
                }
                return list2;
            }
            return null;
        }

        public static List<DB_BaseInfo> GetTopInfoData(string resultXml)
        {
            XElement element = XElement.Parse(resultXml);
            string str = ((XElement) element.FirstNode).Name.ToString();
            return new List<DB_BaseInfo>(from sn in element.Elements("item") select new DB_BaseInfo { Version = sn.Element("version").Value.Trim(), Exchange = sn.Element("exchange").Value.Trim(), Type = sn.Element("type").Value.Trim(), Symbol = sn.Element("symbol").Value.Trim(), Code = sn.Element("code").Value.Trim(), Name = sn.Element("name").Value.Trim(), Trade = sn.Element("trade").Value.Trim(), ChangePercent = sn.Element("changepercent").Value.Trim() });
        }
    }
}
